<?php

class Index extends show
{
    public function __construct()
    {
        date_default_timezone_set('PRC');
        session_start();
        include_once $_SERVER['DOCUMENT_ROOT'].'/Pmodel/dbli.class.php';
        include_once $_SERVER['DOCUMENT_ROOT'].'/Pmodel/NumForChek.class.php';
    }
    
    public function index(){
       
        $apid=$GLOBALS['C']['apid'];
        $scr=$GLOBALS['C']['scr'];
        $urlecode=urlEncode($GLOBALS['C']['urlecode']);
        $quet=$_GET['s'];
        if(!$_GET['code']){
            $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$apid&redirect_uri=$urlecode&response_type=code&scope=snsapi_userinfo&state=$quet#wechat_redirect";
            header("location:$url");
            exit;
        }
       $code= $_GET['code'];
       $tokenurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$apid&secret=$scr&code=$code&grant_type=authorization_code";

      $tokendata=file_get_contents($tokenurl);
      $tokendata=json_decode($tokendata,true); 
       //刷新
       $retokenurl="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=$apid&grant_type=refresh_token&refresh_token={$tokendata['refresh_token']}";
       $retokendata=file_get_contents($retokenurl);
       $retokendata=json_decode($retokendata,true);

       $userinfourl="https://api.weixin.qq.com/sns/userinfo?access_token={$retokendata['access_token']}&openid={$retokendata['openid']}&lang=zh_CN";
       $userdata=file_get_contents($userinfourl);
       $userdata=json_decode($userdata,true);
      // echo'<pre>';
      // print_r($userdata);
       
       //代理ID
       $state=$_GET['state']?$_GET['state']:0;
       $tuid=str_replace('kkxx','',$state);
       $db = new dbli($GLOBALS['C']);
       $sql="select ID from wx_manage where openid='{$userdata['openid']}' limit 0,1";
        
       $reslut=$db->select($sql);
       /* 删除代理不在能访问 */
       $sql="select * from wx_daili where ID=$tuid";
       if($_GET['state'] && !($db->select($sql))){
           die('代理已经失效 请联系客服微信。');
       }
       
       if($reslut[0]['ID']>0 && $userdata['openid']!=''){
          $sql="update wx_manage set nickname='{$userdata['nickname']}',headpic='{$userdata['headimgurl']}' where ID={$reslut[0]['ID']}";
          $db->U_D($sql);
          $_SESSION['ID']=$reslut[0]['ID'];
          header("location:/?G=Index/login");
       }else{
           $sql="INSERT INTO `wx_manage`( `nickname`, `headpic`, `openid`, `money`, `dlID`, `gtime`) VALUES ('{$userdata['nickname']}','{$userdata['headimgurl']}','{$userdata['openid']}',0,$tuid,".time().")";
           $id2=$db->add($sql);
          // echo  $sql;exit;
           if(!$id2){
                $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$apid&redirect_uri=$urlecode&response_type=code&scope=snsapi_userinfo&state=$quet#wechat_redirect";
                header("location:$url");
           }else{
                $_SESSION['ID']=$id2;
                 header("location:/?G=Index/login");
           }
       } 
   
    }

   
    public function login()
    {
        $id2=$_SESSION['ID'];
        if(!$id2){
            header("location:/?G=Index/index");
            exit;
        }
        $str = " ID=$id2 ";
       // error_reporting(E_ALL);
        $db = new dbli($GLOBALS['C']);
     
        //查询规则信息
        $sql="select * from `wx_systemset` where ID=1";
        $ct=$db->select($sql);
        
        $this->assign('ct',$ct[0]['ct']);
        $this->assign('cz',$ct[0]['cz']);
        //查询用户信息
        $sql = "select * from wx_manage where $str";
        $arr = $db->select($sql);

        
        
        $this->assign('user', $arr);
        if($arr[0]['dlID']>0){
            $sql="select name from wx_daili where ID={$arr[0]['dlID']}";
            $arr2 = $db->select($sql);
            $this->assign('dlname', $arr2[0]['name']);
        }
        
       
        
        //查询聊天记录
        $sql="select * from wx_kf where `senderID`=$id2 or `toID`=$id2  order by ID desc limit 0,50";
        
        $data=$db->select($sql);
        $this->assign('msg', $data);
        
        
        
        //查询历史下注记录
       
        $sql="SELECT * FROM `wx_xz` WHERE toID=0 or toID=$id2 ORDER BY id DESC limit 0,30";
         
        $data2=$db->select($sql);
        foreach ($data2 as $key=>$vlaue)
        {
            if($vlaue['senderID']>0){
               $sqlls="select nickname,headpic from wx_manage where ID={$vlaue['senderID']} limit 0,1"; 
               $lsdata=$db->select($sqlls);
               $data2[$key]['nickname']=$lsdata[0]['nickname'];
               $data2[$key]['headpic']=$lsdata[0]['headpic'];
            }
        }
        
        $this->assign('msg2', $data2);
       // print_r($data2);
        //不做改动
        $this->assign('t1',date('m').'/'.date('d'));
        $this->assign('t2',date('m',time()-3600*24).'/'.date('d',time()-3600*24));
        $this->assign('t3',date('m',time()-3600*24*2).'/'.date('d',time()-3600*24*2));
        $this->display();
    }
    
    public function agent()
    {
        $this->display();
    }
    
    
    
}
?>